<template>
  <div class="app-container">
    
    <div class="tabInfo">
    <div class="search">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
        class="demo-form-inline"
      >
        <el-form-item label="名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入表达式名称"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select
            v-model="queryParams.status"
            placeholder="请选择状态"
            clearable
          >
            <el-option
              v-for="dict in sys_common_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="mini" @click="handleQuery">
            <el-icon><Search /></el-icon>搜索</el-button
          >
          <el-button size="mini" @click="resetQuery"
            ><el-icon><Refresh /></el-icon>重置</el-button
          >
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:expression:add']"
            ><el-icon><CirclePlus /></el-icon>新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:expression:edit']"
            ><el-icon><Edit /></el-icon>修改</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:expression:remove']"
          >
            <el-icon><Delete /></el-icon>
            删除</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            size="mini"
            @click="handleExport"
            v-hasPermi="['system:expression:export']"
            ><el-icon><Download /></el-icon>导出</el-button
          >
        </el-col>
        <right-toolbar
          v-model:showSearch="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>
    </div>

    <div class="table" :style="{ height: tabHeight + 'px' }">
      <el-table
        :height="tabHeight"
        v-loading="loading"
        :data="expressionList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="主键" align="center" prop="id" />
        <el-table-column label="名称" align="center" prop="name" />
        <el-table-column label="表达式内容" align="center" prop="expression" />
        <el-table-column label="指定类型" align="center" prop="dataType">
          <template #default="scope">
            <dict-tag :options="exp_data_type" :value="scope.row.dataType" />
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:expression:edit']"
              ><el-icon><Edit /></el-icon>修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:expression:remove']"
            >
              <el-icon><Delete /></el-icon>
              删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination">
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </div>
    <!-- 添加或修改流程达式对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入表达式名称" />
        </el-form-item>
        <el-form-item label="内容" prop="expression">
          <el-input v-model="form.expression" placeholder="请输入表达式内容" />
        </el-form-item>
        <el-form-item label="指定类型" prop="dataType">
          <el-radio-group v-model="form.dataType">
            <el-radio
              v-for="dict in exp_data_type"
              :key="dict.value"
              :label="dict.value"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in sys_common_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { getCommonTableHeight } from "@/utils/commonTableHeight.js";

import { ref, onMounted, reactive } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import {
  listExpression,
  getExpression,
  delExpression,
  addExpression,
  updateExpression,
} from "@/api/flowable/expression";

const loading = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const showSearch = ref(true);
const total = ref(0);
const expressionList = ref([]);
const title = ref("");
const open = ref(false);
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  name: null,
  expression: null,
  status: null,
});
const form = reactive({
  dataType: "fixed",
});

const rules = reactive({});

const formRef = ref(null);
const { proxy } = getCurrentInstance();
console.log(proxy, "proxyproxy");
const { sys_common_status } = proxy.useDict("sys_common_status");
const { exp_data_type } = proxy.useDict("exp_data_type");
const tabHeight = ref(0);

const updateViewportHeight = () => {
  tabHeight.value = getCommonTableHeight();
};
onMounted(() => {
  updateViewportHeight(); // 初始化视口高度
  window.addEventListener("resize", updateViewportHeight); // 监听窗口大小变化
});

onBeforeUnmount(() => {
  window.removeEventListener("resize", updateViewportHeight); // 移除监听
});
onMounted(() => {
  getList();
});

// 取消按钮
const cancel = () => {
  open.value = false;
  this.reset();
};
function reset() {
  this.form = {
    id: null,
    name: null,
    expression: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null,
    status: null,
    remark: null,
  };
  this.resetForm("form");
}

function getList() {
  loading.value = true;
  listExpression(queryParams).then((response) => {
    expressionList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

const handleQuery = () => {
  queryParams.pageNum = 1;
  getList();
};

const resetQuery = () => {
  queryParams.name = null;
  queryParams.expression = null;
  queryParams.status = null;
  handleQuery();
};

const handleSelectionChange = (selection) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
};

const handleAdd = () => {
  resetForm();
  open.value = true;
  title.value = "添加流程达式";
};

const handleUpdate = (row) => {
  const id = row.id || ids.value;
  getExpression(id).then((response) => {
    console.log(response, "responseresponseresponse");
    form.id = response.data.id;
    form.name = response.data.name;
    form.expression = response.data.expression;
    form.createTime = response.data.createTime;
    form.updateTime = response.data.updateTime;
    form.createBy = response.data.createBy;
    form.updateBy = response.data.updateBy;
    form.status = response.data.status;
    form.remark = response.data.remark;
    open.value = true;
    title.value = "修改流程达式";
  });
};

const submitForm = () => {
  if (form.id != null) {
    updateExpression(form).then((response) => {
      ElMessage.success("修改成功");
      open.value = false;
      getList();
    });
  } else {
    addExpression(form).then((response) => {
      ElMessage.success("新增成功");
      open.value = false;
      getList();
    });
  }
};

const handleDelete = (row) => {
  const ids = row.id || ids.value;
  ElMessageBox.confirm('是否确认删除流程达式编号为"' + ids + '"的数据项？')
    .then(() => {
      delExpression(ids).then(() => {
        getList();
        ElMessage.success("删除成功");
      });
    })
    .catch(() => {});
};

const handleExport = () => {
  // 假设你有一个 download 方法来处理下载
  // download('system/expression/export', {
  //   ...queryParams
  // }, `expression_${new Date().getTime()}.xlsx`)
};

const resetForm = () => {
  form.id = null;
  form.name = null;
  form.expression = null;
  form.createTime = null;
  form.updateTime = null;
  form.createBy = null;
  form.updateBy = null;
  form.status = null;
  form.remark = null;
};
</script>

<style>

.demo-form-inline .el-input {
  --el-input-width: 220px;
}

.demo-form-inline .el-select {
  --el-select-width: 220px;
}
</style>
